<template>
  <!-- 
    下拉菜单
       van-dropdown-menu
           van-dropdown-item
               title="标题"
               v-model:双向绑定选中后的value
               options="数据[{text:显示文本,value:选中该项后的值}]"
               slot="default(选项内容)/title(标题)"
     索引栏
        van-index-bar    index-list=[A-Z]
            van-index-anchor  index="锚点值与index-list的值对应"
 -->

  <div class="question">
    <van-dropdown-menu>
      <van-dropdown-item ref="dropdown">
        <template #title>
          <div class="title">
            <span class="t1">面试宝典</span><span class="t2">{{ city }}</span>
          </div>
        </template>
        <template #default>
          <van-index-bar :index-list="indexList">
            <!-- v-for="(value,key,index) in 对象" -->
            <div v-for="(value, key, index) in info.citys" :key="index">
              <van-index-anchor class="anchor" :index="key">{{
                key
              }}</van-index-anchor>
              <van-cell
                v-for="(item, index2) in value"
                :key="index2"
                :title="item"
                @click="cellClick(item)"
              ></van-cell>
            </div>
          </van-index-bar>
        </template>
      </van-dropdown-item>
    </van-dropdown-menu>
    <div class="content">
      <div class="tag-area">
        <van-tag
          @click="currIndex = index"
          class="tag"
          v-for="(item, index) in cityPositions[city]"
          :key="index"
          color="#f7f4f5"
          text-color="#9696a6"
          :class="{ active: currIndex === index }"
          >{{ item.name }}</van-tag
        >
      </div>
    </div>
    <div class="middle">
      <div class="top">
        <div class="middle-item">
          <div class="item-top">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
        <div class="middle-item">
          <div class="item-top c2">
            <i class="iconfont">&#xe654;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
      </div>
      <div class="top mt33">
        <div class="middle-item">
          <div class="item-top c3">
            <i class="iconfont">&#xe648;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
        <div class="middle-item">
          <div class="item-top c4">
            <i class="iconfont">&#xe655;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
      </div>
      <!-- circle
      van-circle
         v-model:双向绑定当前值
         rate:目标值(整圈是100)
         speed:多少rate/秒  100  50   
         layer-color:轨道颜色 
         color:进度条颜色
       -->
      <van-circle
        v-if="info.totalCount"
        class="circle"
        v-model="circleValue"
        :rate="(userInfo.correctQuestions.length / info.totalCount) * 100"
        :speed="10"
        layer-color="none"
        :color="gradientColor"
      >
        <div class="circle-content">
          <div class="t1">顺序刷题</div>
          <div class="t2" v-if="info.totalCount">
            {{ userInfo.correctQuestions.length }}/{{ info.totalCount }}
          </div>
        </div>
      </van-circle>
    </div>
    <div class="bottom-num">
      <div class="bottom-txt">累计记录</div>
      <div class="bottom-value" v-if="info.totalCount">
        {{ info.totalCount.toLocaleString() }}
      </div>
    </div>
    <div class="bottom-btn">
      <van-button class="btn" block @click="goInfo">
        <span class="txt">模拟面试</span>
      </van-button>
    </div>
  </div>
</template>
<script>
import { interviewFilters } from '@/api/question.js'
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState(['userInfo'])
  },
  data () {
    return {
      city: '全国',
      indexList: [], // citys.所有的key转换成数组
      info: '',
      cityPositions: '',
      currIndex: 0,
      circleValue: 0,
      //       border-image: linear-gradient(135deg, rgba(228,1,55,0.30), rgba(228,1,55,0.70) 99%) 13 13;
      // border-radius: 50%;

      gradientColor: {
        '100%': 'rgba(228,1,55,0.30)',
        '0%': 'rgba(228,1,55,0.70)'
      }
    }
  },
  async beforeCreate () {
    const res = await interviewFilters()
    this.info = res.data.data
    // Object.keys(对象) 对象的所有的key转换成的数组
    // Object.values(对象) 对象的所有的value转换成的数组
    // Object.entries(对象) [[key,value],[key,value]]
    this.indexList = Object.keys(res.data.data.citys)
    this.cityPositions = res.data.data.cityPositions
    window.console.log(res)
  },
  created () {},
  mounted () {
    document.body.style.backgroundColor = '#f7f4f5'
  },
  beforeDestroy () {
    document.body.style.backgroundColor = '#ffffff'
  },
  methods: {
    cellClick (str) {
      this.city = str
      this.currIndex = 0
      // this.$refs.dropdown===dropdown-item组件的实例对象
      this.$refs.dropdown.toggle(false)
    },
    goInfo () {
      const _type = this.cityPositions[this.city][this.currIndex].id
      this.$router.push(
        '/home/questionInfo?type=' + _type + '&city=' + this.city
      )
    }
  }
}
</script>
<style lang="less" scoped>
.question {
  .title {
    .t1 {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .t2 {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: center;
      color: #545671;
      line-height: 16px;
    }
  }
  .anchor {
    background-color: #f7f4f5;
  }
  .content {
    padding: 10px @p15;
    background-color: #fff;
    .tag-area {
      overflow: auto;
      display: flex;
      align-items: center;
      .tag {
        // flex:多余空间怎么分配0 空间不够怎么怎么1 auto;
        flex: 0 0 auto;
        padding: 5px 8px;
        margin-right: 10px;
      }
      .active {
        background-color: #00b8d4 !important;
        color: #fff !important;
      }
    }
  }
  .middle {
    padding: 16px 41px;
    margin-top: 20px;
    background-color: #fff;
    position: relative;
    .top {
      display: flex;
      justify-content: space-between;
      text-align: center;
      .item-top {
        width: 44px;
        height: 44px;
        background: linear-gradient(180deg, #ff8080, #ff4949);
        border-radius: 50%;
        line-height: 44px;
        color: #fff;
        .iconfont {
          font-size: 24px;
        }
      }
      .c2 {
        background: linear-gradient(180deg, #ffda05, #ffb302);
      }
      .c3 {
        background: linear-gradient(180deg, #00ddec, #00b8d4);
      }
      .c4 {
        background: linear-gradient(180deg, #3ee5b1, #1dc779);
      }
      .item-bottom {
        margin-top: 10px;
        font-size: 12px;
      }
    }
    .mt33 {
      margin-top: 33px;
    }
    .circle {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 120px;
      height: 120px;
      background-image: url(~@/assets/circle.png);
      background-size: 100%;
      padding: 10px;
      .circle-content {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        background: linear-gradient(180deg, #ff6f92, #e40137);
        color: #fff;
        font-size: 14px;
        .t1 {
          padding-top: 36px;
        }
      }
    }
  }
  .bottom-num {
    margin-top: 20px;
    background-color: #fff;
    text-align: center;
    .bottom-txt {
      font-size: 16px;
      padding: 20px 0 20px;
    }
    .bottom-value {
      font-size: 24px;
      font-weight: 600;
      padding-bottom: 20px;
    }
  }
  .bottom-btn {
    padding: 0 @p15;
  }

  .btn {
    margin-top: 20px;
    background-color: #00b8d4;
    .txt {
      color: #ffffff;
    }
  }
}
</style>
